<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>姓名案例-计算属性实现</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    姓：<input name="姓" v-model:value="firstName"> <br>
    名：<input name="名" v-model:value="lastName"> <br>
    <h4>全名：{{fullName}}</h4>

</div>
<script type="text/javascript">
    Vue.config.productionTip = false

    const vm = new Vue({
        el: "#root",
        data: {
            firstName: "周",
            lastName: "围"
        },
        computed: {
            fullName: {
                // 当有人读取fullName，get就会被调用，且返回值就作为fullName的值
                // get什么时候被调用？
                // 1、初次读取fullName时。2、所依赖的数据发生变化时
                get() {
                    // 此处的this是vm
                    return this.firstName + "-" + this.lastName
                },
                set(value){
                    const arr = value.split("-")
                    this.firstName = arr[0]
                    this.lastName = arr[1]

                }
            }
        }
    })
</script>

</body>
</html>